package com.scs.application.modules.upms.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.scs.application.core.model.DataEntity;
import com.scs.application.core.utils.StringUtils;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * @Description: 代码生成表
 */
@Data
@Accessors(chain = true)
@TableName("gen_table")
public class GenTable extends DataEntity {

    private String datasourceId;

    private String tableName;

    private String className;

    private String comments;

    private String parentTableName;

    private String parentTableFkName;

    private String tplCategory;

    private String packageName;

    private String moduleName;

    private String subModuleName;

    private String functionName;

    private String functionNameSimple;


    private String functionAuthor;

    private String genBaseDir;

    private String options;


    @TableField(exist = false)
    private List<GenTableColumn> columns;

    @JSONField(serialize = false, deserialize = false)
    @TableField(exist = false)
    private List<GenTable> childTableList;


    /**
     * 获取导入依赖包字符串
     *
     * @return
     */
    @JsonIgnore
    @JSONField(deserialize = false, serialize = false)
    public List<String> getImportList() {
        List<String> importList = Lists.newArrayList(); // 引用列表
        for (GenTableColumn column : getColumns()) {
            if (column.getIsNotBaseField()) {
                // 导入JSR303、Json等依赖包
                for (String ann : column.getAnnotationList()) {
                    if (!importList.contains(StringUtils.substringBefore(ann, "("))) {
                        importList.add(StringUtils.substringBefore(ann, "("));
                    }
                }
            }
        }
        return importList;
    }


    /**
     * 是否存在父类
     *
     * @return
     */
    @JsonIgnore
    @JSONField(deserialize = false, serialize = false)
    public Boolean getParentExists() {
        return StringUtils.isNotBlank(parentTableName) && StringUtils.isNotBlank(parentTableFkName);
    }


    public String getSubModelName() {
        return StringUtils.defaultString(this.subModuleName);
    }
}
